"""
Case Type   : gs_checkse工具功能
Case Name   : 验证通过-h参数指定需要检查的节点ip
Create At   : 2025/02/13
Owner       : sungang
Description :
    1、分别检测主机，备一、备二gs_checkse -i A -h ip1,ip2,ip3  --detail
    2、修复主机，备一、备二安全项gs_checkse -i B -h  ip1,ip2,ip3
    3、在主机执行指定备一gs_checkse -i A -h ip2 --detail
    4、在主机执行指定备一gs_checkse -i B -h ip2 --detail
Expect      :
    1、主机、备一、备二安全项检测成功
    2、修复成功显示normal
    3、检测备一ip2的安全项
    4、修复备一ip2的安全项
History     :
"""
import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from yat.test import Node
from yat.test import macro

pri_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != pri_sh.get_node_num(), "需一主两备环境，否则不执行")
class GS_CHECKSE(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.sh_sta1 = Node('Standby1DbUser')
        self.sh_sta2 = Node('Standby2DbUser')
        self.user_node = Node('PrimaryDbUser')
        self.sh_sta1_ip = self.sh_sta1.ssh_host
        self.sh_sta2_ip = self.sh_sta2.ssh_host
        self.user_node_ip = self.user_node.ssh_host
        self.log.info(self.sh_sta1_ip, self.sh_sta2_ip, self.user_node_ip)
        self.common = Common()
        self.env_path = macro.DB_ENV_PATH

    def test_gscheckse(self):
        text = '-----step1:分别检测主机，备一、备二gs_checkse -i A -h ip1,ip2,ip3;expect:主机、备一、备二安全项检测成功-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i A -h {self.sh_sta1_ip},{self.sh_sta2_ip},{self.user_node_ip}"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn("A1. [ Checking Connection configuration ]", sql_res, '执行失败' + text)
        self.assertIn(f"Total numbers:10. Abnormal numbers:", sql_res, '执行失败' + text)

        text = '-----step2:修复主机，备一、备二安全项gs_checkse -i B -h  ip1,ip2,ip3;expect:修复成功显示normal-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i B -h {self.sh_sta1_ip},{self.sh_sta2_ip},{self.user_node_ip}"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn("B1. [ Set Connection configuration ]", sql_res, '执行失败' + text)
        self.assertIn("Total numbers:10. Abnormal numbers:0. Warning numbers:0", sql_res, '执行失败' + text)

        text = '-----step3:在主机执行指定备一gs_checkse -i A -h ip2 --detail;expect:检测备一ip2的安全项成功-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i A -h {self.sh_sta2_ip} --detail"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn("A1. [ Checking Connection configuration ]", sql_res, '执行失败' + text)
        self.assertIn(f"{self.sh_sta2_ip}", sql_res, '执行失败' + text)

        text = '-----step4:在主机执行指定备一gs_checkse -i B -h ip2 --detail;expect:修复备一ip2的安全项成功-----'
        self.log.info(text)
        sql_cmd = f"source {self.env_path};gs_checkse -i B -h {self.sh_sta2_ip} --detail"
        self.log.info(sql_cmd)
        sql_res = self.common.get_sh_result(self.user_node, sql_cmd)
        self.log.info(sql_res)
        self.assertIn("B1. [ Set Connection configuration ]", sql_res, '执行失败' + text)
        self.assertIn("Total numbers:10. Abnormal numbers:0. Warning numbers:0", sql_res, '执行失败' + text)

    def tearDown(self):
        self.log.info(f"无需清理环境")
        self.log.info(f'----{os.path.basename(__file__)} end----')
